home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 April: Mac OS SDK / Dev.CD Apr 98 SDK1.toast / Development Kits (Disc 1) / QuickDraw 3D / Samples / SampleCode / BoxMooV / sources / BoxMooV_document.c next >
Encoding:
C/C++ Source or Header  |  1997-08-14  |  7.2 KB  |  277 lines  |  [TEXT/CWIE]

  1. /*  BoxPaint_document.c                                                                            
  2.  
  3.     This contains all the document-specific code.
  4.                                                                                     
  5.     Rick Evans    - September 9, 1996    derived from BoxPaint_document.c
  6.     Michael Bishop - August 21 1996                                                    
  7.     Nick Thompson
  8.     Robert Dierkes                                                                                
  9.     (c)1994-96 Apple computer Inc., All Rights Reserved                                
  10.  
  11. */
  12.  
  13.  
  14. /* --------------------------------------------------------------------
  15. ** Includes
  16. */
  17. #include    "BoxMooV_document.h"
  18. #include    "BoxPaint_Support.h"
  19. #include    "BoxMooV_window.h"
  20. #include    "BoxMooV_texture.h"
  21.  
  22. /* QuickDraw 3D stuff */
  23. #include "QD3DMath.h"
  24. #include "QD3DTransform.h"
  25. #include "QD3DGroup.h"
  26. #include "QD3DShader.h"
  27. #include "QD3DStorage.h"
  28. #include "QD3DIO.h"
  29.  
  30. /* --------------------------------------------------------------------
  31. ** Local Functions
  32. */
  33. void        Document_Init( DocumentHdl theDocument) ;
  34. void         Texture_DeletePixmap(TAnimatedTextureHdl pAnimTxtr);
  35.  
  36.  
  37. /* -------------------------------------------------------------------------------------------
  38. ** Document_Init
  39. ** Sets All Variables and objects common to all documents
  40. */
  41.  
  42. void Document_Init( 
  43.         DocumentHdl theDocument) 
  44. {
  45.     TQ3Point3D        myOrigin = { 0, 0, 0 } ;
  46.     
  47.     if(theDocument == NULL) goto bail;
  48.     
  49.     (**theDocument).fGroupScale = 1;                
  50.     (**theDocument).fGroupCenter = myOrigin ;            
  51.     
  52.     /*    Get a window for it    */
  53.     if(((**theDocument).fWindow = Window_New()) == NULL) {
  54.         DisposeHandle( (Handle)theDocument ) ;
  55.     }
  56.     else 
  57.     {            
  58.         
  59.         /*  sets up the 3d data for the scene */
  60.         /*  Create view for QuickDraw 3D. */
  61.         (**theDocument).fView = MyNewView( (**theDocument).fWindow ) ;
  62.  
  63.         /*  the main display group: */
  64.         (**theDocument).fModel = MyNewModel() ;
  65.     
  66.         /*  the drawing styles: */
  67.         (**theDocument).fInterpolation = Q3InterpolationStyle_New(kQ3InterpolationStyleNone) ;
  68.         (**theDocument).fBackFacing = Q3BackfacingStyle_New(kQ3BackfacingStyleRemove ) ;
  69.         (**theDocument).fFillStyle = Q3FillStyle_New(kQ3FillStyleFilled ) ;
  70.  
  71.         /*  set the rotation matrix the identity matrix */
  72.         Q3Matrix4x4_SetIdentity(&(**theDocument).fRotation);    
  73.             
  74.         /* store a reference to the document record in the window's refcon */
  75.         SetWRefCon ((**theDocument).fWindow, (long)theDocument ) ;
  76.     }
  77. bail:
  78. ;
  79. }
  80.  
  81. /*    --------------------------------------------------------------------
  82. **    Texture_DeletePixMap
  83. **    Deallocates memory
  84. */
  85. void Texture_DeletePixmap(
  86.         TAnimatedTextureHdl pAnimTxtr)
  87. {
  88.     /*    if there is an image dispose of the storage object    */
  89.     if ((**pAnimTxtr).fStoragePixmap.image != NULL)
  90.     {
  91.         Q3Object_Dispose((**pAnimTxtr).fStoragePixmap.image);
  92.         (**pAnimTxtr).fStoragePixmap.image = NULL;
  93.     }
  94.     
  95.     
  96. }
  97.  
  98. /* -------------------------------------------------------------------------------------------
  99. ** Document_GetFromWindow
  100. ** Gets the document associated with a window
  101. */
  102.  
  103. DocumentHdl Document_GetFromWindow( 
  104.         WindowPtr theWindow)
  105. {
  106.     if (theWindow != NULL)
  107.         return    (DocumentHdl) GetWRefCon ( theWindow );
  108.     else return NULL;
  109. }
  110.  
  111.  
  112. /* -------------------------------------------------------------------------------------------
  113. ** Document_Delete
  114. ** Destroys and deletes all the data in a document
  115. */
  116.  
  117. void Document_Delete(
  118.         DocumentHdl theDocument)
  119. {
  120.     if (theDocument != NULL) {
  121.     
  122.         Q3Object_Dispose((**theDocument).fView) ;                /*  the view for the scene */
  123.         Q3Object_Dispose((**theDocument).fModel) ;                /*  object in the scene being modelled */
  124.         Q3Object_Dispose((**theDocument).fInterpolation) ;        /*  interpolation style used when rendering */
  125.         Q3Object_Dispose((**theDocument).fBackFacing) ;            /*  whether to draw shapes that face away from the camera */
  126.         Q3Object_Dispose((**theDocument).fFillStyle) ;            /*  whether drawn as solid filled object or decomposed to components */
  127.         
  128.         Texture_Delete((**theDocument).fTexture);
  129.         
  130.         Window_Delete ( (**theDocument).fWindow ) ;                /*    get rid of the window    */
  131.  
  132.         /*    Do this last    */
  133.         DisposeHandle( (Handle)theDocument ) ;
  134.     }
  135. }
  136.  
  137.  
  138. /* -------------------------------------------------------------------------------------------
  139. **    Document_Draw
  140. **    Draws the document in the current DrawContext
  141. */
  142.  
  143. TQ3Status Document_Draw( DocumentPtr theDocument )
  144. {    
  145.     TQ3Status        status = kQ3Failure;
  146.     
  147.     if(theDocument == NULL) goto bail;
  148.     
  149.     if ((status = Q3View_StartRendering(theDocument->fView)) != kQ3Failure)
  150.     do {
  151.         Document_SubmitScene( theDocument ) ;
  152.     } while (Q3View_EndRendering(theDocument->fView) == kQ3ViewStatusRetraverse );
  153.     
  154. bail:
  155.     
  156.     return status ;
  157.  
  158. }
  159.  
  160. /* -------------------------------------------------------------------------------------------
  161. **    Document_New
  162. **    Loads a new Document with a blank (white) canvas for a texture
  163. */
  164.  
  165. DocumentHdl Document_New(
  166.         void)
  167. {
  168.     DocumentHdl theDocument = (DocumentHdl)NewHandle( sizeof(DocumentRec)) ;
  169.     
  170.     if( theDocument != NULL )
  171.     {
  172.  
  173.         HLock( (Handle)theDocument ) ;
  174.             
  175.         /* init the field to some reasonable values */
  176.  
  177.         Document_Init( theDocument) ;
  178.  
  179.         /*    By passing NULL as the first parameter, we tell    the function to
  180.             make a blank offscreen    */
  181.         (**theDocument).fTexture = Texture_New();
  182.  
  183.         /*    Add it to the model    */
  184.         Texture_AddToGroup((**theDocument).fTexture, (**theDocument).fModel) ;
  185.  
  186.         AdjustCamera(*theDocument) ;
  187.  
  188.         HUnlock( (Handle)theDocument ) ;
  189.  
  190.     }
  191.  
  192.     return theDocument ;
  193. }
  194.  
  195. /* -------------------------------------------------------------------------------------------
  196. **    Document_Open
  197. **    Loads a new Document with a Pict for a texture
  198. */
  199.  
  200. DocumentHdl Document_Open(
  201.         void)
  202. {
  203.     DocumentHdl theDocument;
  204.     
  205.     if ( (theDocument = (DocumentHdl)NewHandle( sizeof(DocumentRec))) != NULL )
  206.     {
  207.             
  208.         /* lock the handle and init the field to some reasonable values */
  209.  
  210.         Document_Init(theDocument) ;
  211.  
  212.         (**theDocument).fTexture = Texture_New();
  213.  
  214.         Texture_AddToGroup( (**theDocument).fTexture, (**theDocument).fModel ) ;
  215.  
  216.         HLock( (Handle)theDocument ) ;
  217.  
  218.         AdjustCamera(*theDocument) ;
  219.                 
  220.         HUnlock( (Handle)theDocument ) ;
  221.         
  222.     }    
  223.     
  224.     return theDocument ;
  225.     
  226. }
  227.     
  228. /* -------------------------------------------------------------------------------------------
  229. **    Document_SubmitScene
  230. **    if you make a function like this, you can easily use it inside a
  231. **    Rendering or Picking or BoundingSphere/Box loop
  232. **    (See Document_Draw)
  233. */
  234.  
  235. TQ3Status Document_SubmitScene( 
  236.         DocumentPtr theDocument ) 
  237. {        
  238.     TQ3Vector3D                globalScale;
  239.     TQ3Vector3D                globalTranslate;
  240.     
  241.     globalScale.x = globalScale.y = globalScale.z = theDocument->fGroupScale;
  242.     globalTranslate = *(TQ3Vector3D *)&theDocument->fGroupCenter;
  243.     Q3Vector3D_Scale(&globalTranslate, -1, &globalTranslate);
  244.     Q3Style_Submit(theDocument->fInterpolation, theDocument->fView);
  245.     Q3Style_Submit(theDocument->fBackFacing , theDocument->fView);
  246.     Q3Style_Submit(theDocument->fFillStyle, theDocument->fView);
  247.         
  248.     Q3MatrixTransform_Submit( &theDocument->fRotation, theDocument->fView);
  249.         
  250.     Q3ScaleTransform_Submit(&globalScale, theDocument->fView);
  251.     Q3TranslateTransform_Submit(&globalTranslate, theDocument->fView);
  252.     Q3DisplayGroup_Submit( theDocument->fModel, theDocument->fView);
  253.     
  254.     return kQ3Success ;
  255. }
  256.  
  257.  
  258.             
  259. /* -------------------------------------------------------------------------------------------
  260. **    Document_Animate
  261. **    Advance the movie one frame
  262. **    Rotate the box a little
  263. */
  264.  
  265. void Document_Animate(
  266.         DocumentHdl theDocument)
  267. {
  268.     TQ3Matrix4x4        tmp;
  269.     
  270.     Texture_NextFrame((**theDocument).fTexture);
  271.     
  272.     Q3Matrix4x4_SetRotate_XYZ(&tmp, 0.025, 0.03, 0.02);
  273.     Q3Matrix4x4_Multiply(&(**theDocument).fRotation, &tmp,
  274.                             &(**theDocument).fRotation);
  275.  
  276. }
  277.